package com.service.impl;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bean.ArticleContext;
import com.dao.ArticleContextDao;
import com.service.ArticleContextService;
import com.util.CacheUtil;
import com.util.LoggerUtil;
import com.util.Pager;
import com.util.Redis;
import org.apache.commons.lang3.StringUtils;
@Service
public class ArticleContextServiceImpl implements ArticleContextService{
	public static Logger logger=Logger.getLogger(ArticleContextServiceImpl.class);
	@Autowired
	private ArticleContextDao articleContextDao;

	@Override
	public Map<String, Object> selectListByPageAreaArticle(Integer curPage,
			Integer pageSize, String areaId,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			String keys=logs.getString("keys");
			Pager<ArticleContext> pager=articleContextDao.selectListByPageAreaArticle(curPage, pageSize,areaId);
		
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				logs.put("result", "false(no data)");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
			if(StringUtils.isNotEmpty(keys)){
				Redis.setRedisData(keys, CacheUtil.getValue(map));
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
	
	@Override
	public Map<String, Object> selectFollowArticle(Integer curPage,
			Integer pageSize, String id,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			Pager<ArticleContext> pager=articleContextDao.selectFollowArticle(curPage, pageSize,id);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}
	@Override
	public Map<String, Object> selectOfficialListArticle(Integer curPage,
			Integer pageSize, String userId,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			Pager<ArticleContext> pager=articleContextDao.selectOfficialListArticle(curPage,pageSize,userId);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}

	@Override
	public Map<String, Object> updateZanNum(String id, String user_id) {
		return articleContextDao.updateZanNum(id, user_id);
	}

	@Override
	public Map<String, Object> findById(String id,String userId,JSONObject logs) {
		return articleContextDao.findById(id,userId,logs);
	}

	@Override
	public Map<String, Object> selectFirstPageArticle(Integer pageSize,Integer curPage,Integer rating,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		String keys=logs.getString("keys");
		try {
			Pager<ArticleContext> pager=articleContextDao.selectFirstPageArticle(curPage,pageSize,rating);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				if(StringUtils.isNotEmpty(keys)){
					Redis.setRedisData(keys, CacheUtil.getValue(map));
				}
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}

	@Override
	public Map<String, Object> selectByFindRelate(String articleId, JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			List<ArticleContext> data=articleContextDao.selectByFindRelate(articleId);
			if(data!=null&&data.size()>0){
				map.put("result", "success");
				map.put("rows", data);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
	
}
